#include "q_painter.h"


q_painter::q_painter(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui_q_painter)
{
    ui->setupUi(this);

    button_= new QPushButton("移动", this);
    button_->setGeometry(50, 500, 80, 50);



    connect(button_, &QPushButton::clicked, this, &q_painter::on_button_clicked);
}

q_painter::~q_painter()
{
    delete ui; 
}
void q_painter::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    /**
     * @brief 构造函数
     * @param parent 父窗口指针，默认为nullptr
     */
    QPen pen;
    /**
     * @brief 析构函数
     */
    pen.setColor(QColor(255, 0, 0));
    pen.setWidth(5);

    painter.setPen(pen);
    painter.drawLine(20, 20, 100, 20);

    pen.setColor(QColor(255, 255, 0));
    pen.setStyle(Qt::DotLine); 
    painter.setPen(pen);
    painter.drawLine(20, 50, 150, 50);
    
    pen.setColor(QColor(0, 0, 255));
    pen.setStyle(Qt::DashDotDotLine);
    painter.setPen(pen);
    painter.drawLine(20, 80, 200, 80);
    
    // rgba(233, 227, 197, 1)
 
    QBrush brush(QColor(233, 227, 197));
    painter.setBrush(brush);
    pen.setColor(QColor(0, 255, 0));
    pen.setStyle(Qt::DashLine);
    painter.setPen(pen);
    painter.drawRect(20, 110, 100, 50);


    pen.setColor(QColor(0, 0, 0));
    pen.setStyle(Qt::SolidLine);
    painter.setPen(pen);

    brush.setColor(Qt::red);
    brush.setStyle(Qt::CrossPattern);
    painter.setBrush(brush);
    painter.drawEllipse(20, 180, 80, 80);

    // painter.drawPixmap(150, 150, QPixmap("/home/zk/qt-learning/img/huyao.png"));
    painter.drawPixmap(pos_x_, 150, QPixmap("/home/zk/qt-learning/img/huyao.png").scaled(300, 300, Qt::KeepAspectRatio, Qt::SmoothTransformation));


    
}


void q_painter::on_button_clicked()
{
    pos_x_= pos_x_ + 80;
    if (pos_x_ > 600)
    {
        pos_x_ = 150;
    }
    this->update();
}